package com.qcby.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qcby.dao.TicketOrderMapper;
import com.qcby.model.TicketOrder;
import com.qcby.model.dto.OrderQueryDto;
import com.qcby.model.dto.TicketOrderVo;
import com.qcby.query.TicketOrderQuery;
import com.qcby.service.TicketOrderService;
import com.qcby.util.Page;

@Service
public class TicketOrderServiceImpl extends BaseServiceImpl<TicketOrderQuery, TicketOrder> implements
        TicketOrderService {


    private TicketOrderMapper ticketOrderDao;

    @Autowired
    public void setBrandDao(TicketOrderMapper ticketOrderDao) {
        this.ticketOrderDao = ticketOrderDao;
        this.baseDao = ticketOrderDao;
    }

    @Override
    public Integer insertOrder(TicketOrder order) {
        return ticketOrderDao.insertOrder(order);
    }

    @Override
    public void updateByOrderNo(TicketOrder order, Integer oid) {
        ticketOrderDao.updateByOrderNo(order, oid);
    }

    @Override
    public Page<TicketOrderVo> selectCondition(OrderQueryDto queryDto) {
        Integer pageNo = queryDto.getPageNo();
        Integer pageSize = queryDto.getPageSize();
        Integer startNum = (pageNo - 1) * pageSize;


        Integer uid = queryDto.getUid();
        Integer cId = queryDto.getCId();
        Integer hId = queryDto.getHId();
        String movieName = queryDto.getMovieName();
        List<String> seat = queryDto.getSeatList();
        String beginDay = queryDto.getBeginDay();
        String endDay = queryDto.getEndDay();

        List<TicketOrderVo> ticketOrderVoList = ticketOrderDao.selectCondition(uid, cId, hId, movieName, seat, beginDay,
            endDay, startNum, pageSize);
        Integer count = ticketOrderDao.selectConditionCount(uid, cId, hId, movieName, seat, beginDay, endDay);

        Page<TicketOrderVo> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);
        //把总记录数设置给page对象
        page.setTotalCount(count);
        page.setList(ticketOrderVoList);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }
}
